﻿namespace My.Hydrator.Formatters
{
    using System;
    using System.ComponentModel.Composition;
    using System.ComponentModel.DataAnnotations;

    [Export(typeof(IDataTypeFormatter))]
    public class DateTimeDataTypeFormatter : BaseDataTypeFormatter, IDataTypeFormatter
    {
        private readonly DateTime minDate;

        private readonly DateTime maxDate;

        public DateTimeDataTypeFormatter()
            : this(new DateTime(1900, 1, 1), DateTime.Now)
        {
        }

        public DateTimeDataTypeFormatter(DateTime minDate, DateTime maxDate)
        {
            this.minDate = minDate;
            this.maxDate = maxDate;
        }

        public virtual DataTypeAttribute FormatterDataType
        {
            get { return new DataTypeAttribute(DataType.DateTime); }
        }

        public virtual object GetFormat()
        {
            var timeSpan = this.maxDate - this.minDate;
            var days = this.Random.Next(0, timeSpan.Days);

            return this.minDate.AddDays(days);
        }
    }
}
